/* MenuView.cpp
 * The first view to be loaded. Takes the user to the other views
 *
 * Last Modified: 16.01.2014
 * Author: Guilherme S.G.
 *
 * TODO: Implement a more generic/intelligent config file for each view
 */

#include "MenuView.h"
#include "Game.h"


MenuView::MenuView() : View()
{
    al_resize_display(controller->getDisplay(), 640, 480);
    al_set_window_position(controller->getDisplay(), 275, 225);
    
    ResourceManager *resources = controller->getResources();
    
    background = resources->loadImage("assets/backgrounds/menu-bg.png", "menu-bg");
    menuFont = resources->loadFont("assets/fonts/njnaruto.ttf", 24, 0, "njnaruto-24");
    itemFont = resources->loadFont("assets/fonts/ArialBlack.ttf", 16, 0, "arialblack-16");
    
    //Menu options
    std::fstream menuFile("assets/cfg/menu.cfg", std::fstream::in);
    unsigned long itemCount;
    std::string read;
    
    int centerX = controller->getScreenWidth()/2;
    int centerY = controller->getScreenHeight()/2;
    menuFile >> itemCount;
    
    for(int i = 0; i < itemCount; i++)
    {
        double y = centerY - centerY/itemCount + i*18;
        menuFile >> read;
        
        items.push_back(std::pair<std::string, Coordinate>(read,Coordinate(centerX-20,y)));
    }
    
    //Cursor
    ALLEGRO_BITMAP *cursorIMG = controller->getResources()->loadImage("assets/misc/cursor-kunai.png", "cursor-kunai");
    cursor = new Sprite("cursor");
    cursor->setFlag(ROLLING);
    cursor->setImage(cursorIMG);
    cursor->setParameters(3, 1, 0, 3, -2);
    cursor->setPosition(items.begin()->second.x, items.begin()->second.y);
    cursor->clip(1, 4, 66, 32);
    
    selectedItem = 0;
    
    al_reserve_samples(3);
    menuMusic = resources->loadSound("assets/audio/ost2-main-menu.ogg", "menu-music");
    selectFx = resources->loadSound("assets/audio/jutsu_kling.ogg", "menu-selectFx");
    cursorMoveFx = resources->loadSound("assets/audio/fast_cut.ogg", "menu-cursorMoveFx");
    
    al_play_sample(menuMusic, 1, 0, 1, ALLEGRO_PLAYMODE_LOOP, &menuMusicID);
}

MenuView::~MenuView()
{
    al_stop_sample(&menuMusicID);
}

void MenuView::handleEvents(ALLEGRO_EVENT *event)
{
    if(event->type == ALLEGRO_EVENT_KEY_DOWN)
    {
        switch (event->keyboard.keycode)
        {
            case ALLEGRO_KEY_UP:
                selectedItem--;
                break;
            case ALLEGRO_KEY_DOWN:
                selectedItem++;
                break;
        }
        al_play_sample(cursorMoveFx, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
    }
    else if(event->type == ALLEGRO_EVENT_KEY_UP)
    {
        switch (event->keyboard.keycode)
        {
            case ALLEGRO_KEY_ENTER:
                switch (selectedItem)
                {
                    case 0:
                        controller->setNextView(SELECTION);
                        break;
                    case 1:
                        controller->setNextView(CALIBRATION);
                        break;
                    case 2:
                        controller->setNextView(INSTRUCTIONS);
                        break;
                    case 3:
                        controller->setNextView(CREDITS);
                        break;
                    case 4:
                        controller->setNextView(END);
                        break;
                }
                al_play_sample(selectFx, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
                break;
                
            case ALLEGRO_KEY_ESCAPE:
                controller->setNextView(END);
                break;
        }
    }
}

void MenuView::update()
{
    std::vector<std::pair<std::string,Coordinate> >::iterator item;
    
    if(selectedItem <= 0)
        selectedItem = 0;
    if(selectedItem >= items.size())
        selectedItem = (int)items.size()-1;
    
    item = items.begin()+selectedItem;
    
    cursor->setPosition(item->second.x-62, item->second.y-5);
    cursor->update();
}

void MenuView::render()
{
    int centerX = controller->getScreenWidth()/2;
    
    al_clear_to_color(al_map_rgb(255, 0, 0));
    al_draw_bitmap(background, 0, 0, 0);
    
    al_draw_text(menuFont, al_map_rgb(255, 255, 255), centerX-2, 35-2, ALLEGRO_ALIGN_CENTRE, "JUTSU BATTLE");
    al_draw_text(menuFont, al_map_rgb(255, 255, 255), centerX+2, 35+2, ALLEGRO_ALIGN_CENTRE, "JUTSU BATTLE");
    al_draw_text(menuFont, al_map_rgb(255, 128, 0), centerX, 35, ALLEGRO_ALIGN_CENTRE, "JUTSU BATTLE");
    
    //Menu options
    std::vector<std::pair<std::string,Coordinate> >::iterator iter;
    for(iter = items.begin(); iter != items.end(); iter++)
    {
        std::string text = iter->first;
        Coordinate coord = iter->second;
        
        al_draw_text(itemFont, al_map_rgb(0, 0, 0), coord.x+3, coord.y+1, ALLEGRO_ALIGN_LEFT, text.c_str());
        al_draw_text(itemFont, al_map_rgb(255, 255, 255), coord.x, coord.y, ALLEGRO_ALIGN_LEFT, text.c_str());
    }
    
    cursor->animate();
}
